 
import { WorldState } from "GameLoop/WorldState/worldstate";
import { spawnPriority } from "utils/constants/priorityconstant";
import { ATaskAdapter } from "./TaskAdapter";
import { TeamTaskAdapter } from "./TaskAdapter/TeamTaskAdapter";
import { CreepTaskAdapter } from "./TaskAdapter/CreepTaskAdapter";
import { RoomTaskAdapter } from "./TaskAdapter/RoomTaskAdapter";
import { ReactionTaskAdapter } from "./TaskAdapter/ReactionTaskAdapter";
import { BoostTaskAdapter } from "./TaskAdapter/BoostTaskAdapter";
import { TaskConditions } from "./ConditionOfComplete/Conditions";
import { PowerCreepTaskAdapter } from "./TaskAdapter/PowerCreepTaskAdapter";
import { TerminalTaskAdapter } from "./TaskAdapter/TerminalTaskAdapter";
import { FactoryTaskAdapter } from "./TaskAdapter/FactoryTaskAdapter";
import { CoreTaskAdapter } from "./TaskAdapter/CoreTaskAdapter";

import { ETaskState } from "GameLoop/DecisionMaking/TaskDeclare/type";
import { TowerTaskAdapter } from "./TaskAdapter/TowerTaskAdapter";
 
// import profiler from "screeps-profiler";
 

// const roleorder:Record<RoleName,(CreepTaskType|TeamTaskType)[]>={
//     upgrader: ["upgrade", "build"],
//     worker: ["build", "upgrade"],
//     mine: ["harvest"],
//     porter: ["harvest"],
//     filler: [],
//     carry: ["fill"],
//     scout: ["scout"],
//     claimer: ["claim"],
//     idaRed: ["defend"],
//     idaBlue: ["defend"],
//     idaHeal: ["defend"],
//     defRed: ["defend"],
//     defBlue: ["defend"]
// } 
/**
 * 适配层
 * 承上启下  对任务 和执行实体之间进行绑定
 */ 
/*@profile*/
export class AdaptationLayer
{
    private worldstate!:WorldState;
    
    private adapter!:ITaskAdapter;
    
    public get getWorldState():WorldState
    {
        return this.worldstate
    }
    public init(world:WorldState)
    {

        this.worldstate = world;
   
        // this.adapter["creep"] = 
        this.adapter={} as any;
        this.adapter['room'] = new RoomTaskAdapter(this,world);
        this.adapter['creep'] = new CreepTaskAdapter(this,world);
        this.adapter['team'] = new TeamTaskAdapter(this,world);
        this.adapter['react'] = new ReactionTaskAdapter(this,world);
        this.adapter['boost'] = new BoostTaskAdapter(this,world);
        this.adapter['pc'] = new PowerCreepTaskAdapter(this,world);
        this.adapter['factory'] = new FactoryTaskAdapter(this,world);
        this.adapter['terminal'] = new TerminalTaskAdapter(this,world);
        this.adapter['core'] = new CoreTaskAdapter(this,world);
        this.adapter['tower'] = new TowerTaskAdapter(this,world);
    }

    public run()
    {
        for(let k in Game.rooms)
        {
            // 只执行主房间
            if(Game.rooms[k].getType() ==1)
            {
                this.execute(Game.rooms[k]) ;
            }
        }
    }
    private execute(room:Room)
    {
        // 对房间的任务进行适配
        // room.getEntity()
        const nodememory = room.getNodeMemory();
        for(const t in nodememory.Tasks)
        {
            const adapter = this.adapter[t as keyof ITaskAdapter];
            const tasks = room.getTaskByType(t as TaskType);
            if(adapter)
            {
                adapter.bindTasks(room,tasks as any,this.customConditionCheck.bind(this));
            }
            else
            {
                // 异常
                for(const e of tasks)
                {
                    if(t !== e.type)
                    {
                        room.log("error",'任务添加到错误的分组里');
                    }
                }
               
            }
        }
    }
    
    private customConditionCheck(room:Required<Room>,task:ITask)
    {
        if(task.condition)
        {
            if(ETaskState.Complete ==  TaskConditions.isComplete(room as Required<Room>,task))
            {
                return ETaskState.Complete
            }
        }
        if(task.pause)
        {
            task.pause--;
            return ETaskState.Pause
        }
        // 检查超时
        return TaskConditions.checkTimeout(room as Required<Room>,task)
    }
}